﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EngineLayer
{
    using Services;
    using DBLayer;
    using System.Text.RegularExpressions;
    using EngineLayer.Model;
    
    public class ProductsCatalogsModel : DBEntities
    {
       // private Products_ _ProductService;
        private UpdatesModel updater;

        public ProductsCatalogsModel()
        {
         //   _ProductService = new Products_();
            updater = new UpdatesModel();
        }

        //public Products_ ProductService()
        //{
        //  //  return _ProductService;
        //}

        public bool OnlineUpdate()
        {
            return updater.ProcessUpdate(ElementsToUpdate.PRODUCTOS); 
        }

        private void UpdateProducts(List<string> values)
        {
            try
            {
                foreach(string row in values)
                {
                    if (row != string.Empty)
                    {
                        List<string> RawData = row.Split(',').ToList();
                        DBLayer.Product product;
                        int Id = Convert.ToInt32(Regex.Replace(RawData[0].ToString(), "[^0-9]", ""));
                        //if (_ProductService.Exist(Id))
                        //{
                        //    //actualiza
                        //    product = db.Products.Where(n => n.Id == Id).FirstOrDefault();
                        //    AddValues(ref product, row.Split(',').ToList());
                        //}
                        //else
                        //{
                        //    //agrega nuevo
                        //    product = db.Products.CreateObject();
                        //    product.Id = Id;
                        //    AddValues(ref product, row.Split(',').ToList());
                        //    db.AddToProducts(product);
                       // }
                    }
                }
                SaveChanges();
            }
            catch
            {
             //   System.Windows.Forms.MessageBox.Show(MessageError.FormatFileError, "Error");
            }
        }

        private void AddValues(ref DBLayer.Product value, List<string> column)
        {
            value.Name = column[1];
            value.Description = column[2];
            value.Price = Convert.ToDouble(column[3]);
            value.ProductCategoryId = Convert.ToInt32(column[4]);
            value.Public = true;
        }

        public void SaveProducts()
        {
            if (isEdited<DBLayer.Product>(database.Products))
            {
                if (SaveChanges())
                {
                    if (database.UpdateElement.Where(w => w.Name.Equals("PRODUCTOS")).Select(s => s.Update).First())
                    {
                    //    UpdateElements.UpdateTimeStamp("PRODUCTOS", DateTime.Now);
                   //     ExportProducts();
                    }
                }
            }
        }
    }
}
